package com.example.java8;

public class Lambda {

    public static void main(String[] args){
        Lambda tester = new Lambda();

        // 类型声明
        MathOperation addition = (a,  b) -> a + b;
        // 不用类型声明
        MathOperation subtraction = (a, b) -> a - b;
        // 大括号中的返回语句
        MathOperation multiplication = (int a, int b) -> {
            int c = a * b;
            return c;
        };
        MathOperation multiplication2 = (int a, int b) -> a * b;
        // 没有大括号及返回语句
        MathOperation division = (int a, int b) -> a / b;

        division.operation3(22);

        System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
        System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
        System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
        System.out.println("10 / 5 = " + tester.operate(10, 5, division));

        // 不用括号
        GreetingService greetService1 = message ->System.out.println("Hello " + message);

        // 用括号
        GreetingService greetService2 = (message) ->System.out.println("Hello " + message);
        // 不用括号
        GreetingService greetService3 = System.out::println;


        greetService1.sayMessage("Runoob");
        greetService2.sayMessage("Google");
        greetService3.sayMessage("Hello Google2");
    }

    private int operate(int a, int b, MathOperation mathOperation){
        return mathOperation.operation(a, b);
    }


    @FunctionalInterface
    interface MathOperation {
        int operation(int a, int b);


        default int operation1(int a){
            return a * 2;
        }

        default void operation3(int a){
            System.out.println(2);
        }
    }

    interface GreetingService {
        void sayMessage(String message);
    }


}
